//
//  ZFSegmentControlViewController.m
//  ZFFoundationDemo
//
//  Created by 赵梁 on 2017/1/4.
//  Copyright © 2017年 zify. All rights reserved.
//

#import "ZFSegmentControlViewController.h"

#import <ZFSegmentControl.h>

@interface ZFSegmentControlViewController ()<ZFSegmentControlDataSource,ZFSegmentControlDelegate>
{
    ZFSegmentControl *_segmentControl;
}

@end

@implementation ZFSegmentControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    _segmentControl = ({
        ZFSegmentControl *segment = [[ZFSegmentControl alloc] initWithFrame:CGRectMake(0, 64, width, 36)];
        segment.delegate = self;
        segment.dataSource = self;
        segment.lineColor = [UIColor redColor];
        
        segment;
    });
    
    [self.view addSubview:_segmentControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ZFSegmentControl delegate & dataSource
- (NSInteger)numberOfItemsInZFSegmentControl:(ZFSegmentControl *)control
{
    return 8;
}

- (UIView *)zf_segmentControl:(ZFSegmentControl *)control viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view didSelected:(BOOL)didSelected
{
    UILabel *label;
    if (view == nil) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, control.frame.size.width*0.2, control.frame.size.height)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [NSString stringWithFormat:@"index %ld",(long)index+1];
    }else {
        label = (UILabel *)view;
    }
    
    if (didSelected) {
        label.textColor = [UIColor redColor];
    }else {
        label.textColor = [UIColor darkGrayColor];
    }
    
    return label;
}

- (void)zf_segmentControl:(ZFSegmentControl *)control didSelectedIndex:(NSInteger)index
{
    NSLog(@"点击了 %ld" ,index);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
